﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using TSU.FAMC.ComputerEngineering.Classes.BooleanMaths.Fundamental;

namespace TSU.FAMC.ComputerEngineering.Classes.BooleanMaths.Functions.Readers
{
    /// <summary>
    /// Reads BooleanSystem object from simple text plain format: x1y1z1 f1(t1)f2(t1)\nx2y2z2 f1(t2)f2(t2).
    /// Where fi(tj) - indicator of cube to function beloning
    /// </summary>
    public class BooleanSystemReaderPlain : BooleanSystemReader
    {
        /// <summary>
        /// Input string to read data from.
        /// </summary>
        public string Input { get; set; }

        /// <summary>
        /// Reads BooleanSystem object from text format.
        /// </summary>
        public override void Read()
        {
            if (ReferenceEquals(null, System))
            {
                throw new InvalidOperationException("BooleanSystem object required to read data to.");
            }

            if (Input == String.Empty)
            {
                throw new InvalidOperationException("Input string required.");
            }

            // splitting to lines
            var delimeters = new[] { "\n", "\r\n" };
            var lines = Input.Split(delimeters, Int32.MaxValue, StringSplitOptions.RemoveEmptyEntries);
            if (lines.Length == 0)
            {
                throw new InvalidDataException("Invalid source string: no one line of values.");
            }

            // parsing lines by pattern
            var pattern = new Regex("([01-]+)\\s+([01-]+)");
            
            // parsing line by line
            var dimension = 0;
            var funcNumber = 0;
            var functions = new List<BooleanFunction>();
            foreach (var line in lines)
            {
                if (line == string.Empty) continue;

                var match = pattern.Match(line);
                if (!match.Success)
                {
                    throw new InvalidDataException("Invalid format for source string.");
                }

                // parsing line
                var vector = match.Groups[1].ToString();
                var funcValues = match.Groups[2].ToString();

                // setting dimension of system cubes
                if (dimension == 0)
                {
                    dimension = vector.Length;
                }

                if (vector.Length != dimension)
                {
                    throw new InvalidDataException("Invalid format for source string: different cubes dimensions for one system.");
                }

                // memorizing system dimension
                if (funcNumber == 0)
                {
                    funcNumber = funcValues.Length;

                    // instanciating system functions
                    for (var i = 0; i < funcNumber; i++)
                    {
                        var function = new BooleanFunction();
                        functions.Add(function);
                    }
                }

                if (funcValues.Length != funcNumber)
                {
                    throw new InvalidDataException("Invalid format for source string: different number of functions for one system.");
                }

                // building cube
                Cube cube;
                try
                {
                    cube = new Cube(vector);
                }
                catch (ArgumentException e)
                {
                    throw new InvalidDataException("Invalid format for source string: invalid ternary vector character", e);
                }
                
                // adding cube to the system
                for (var i = 0; i < functions.Count; i++)
                {
                    if (funcValues[i] == '1')
                    {
                        functions[i].Domain1.Add(cube);
                    }
                    else if (funcValues[i] == '0')
                    {
                        functions[i].Domain0.Add(cube);
                    }
                }
            }

            // setting data
            System.Functions = functions;
        }
    }
}
